﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using AlanUtilities;
using DBAccessor.Accessor;
using DBList.Entities;

public partial class Admin_login : System.Web.UI.Page 
{
    private void SaveToCookie(string strname, string strpwd, int nExpires)
    {
        Random random = new Random();
        byte[] tl = new byte[4];
        tl[0] = (byte)((random.Next() % 100) + 1);
        tl[1] = (byte)(strname.Length);
        tl[2] = (byte)((random.Next() % 128) + 1);
        tl[3] = (byte)(strpwd.Length);
        string strEncode = Encoding.BigEndianUnicode.GetString(tl);
        strEncode += strname;
        strEncode += OperateControl.AchieveCharaters(3).ToLower();
        strEncode += strpwd;

        string strDst = OperateControl.StringEncrypt(strEncode);
        string strD2 = HttpUtility.UrlEncode(strDst, Encoding.UTF8);
        HttpCookie htc = new HttpCookie(@"userinfo", strD2);
        htc.Expires = DateTime.Now.AddDays(nExpires);
        Response.Cookies.Set(htc);
    }
    private void PickupFromCookies(ref string strname, ref string strpwd)
    {
	    HttpCookieCollection hcc = Request.Cookies;
	    if(hcc != null){
		    HttpCookie hc = hcc.Get(@"userinfo");
		    if (hc != null)
		    {
			    string str = hc.Value;
			    if (str != null && str.Length > 0)
			    {
				    str = HttpUtility.UrlDecode(str, Encoding.UTF8);

				    string strSrc = OperateControl.StringDecrypt(str);
				    if(strSrc != null && strSrc.Length > 0){
					    byte[] bba = Encoding.BigEndianUnicode.GetBytes(strSrc);
					    if(bba != null){
						    byte[] bname = new byte[bba[1]*2];
						    byte[] bpwd = new byte[bba[3]*2];
						    if(bname!=null && bpwd != null){
							    int nPos = 4;
							    Array.Copy(bba, nPos, bname, 0, bba[1] * 2);
							    nPos += bba[1] * 2 + 3 * 2;
							    Array.Copy(bba, nPos, bpwd, 0, bba[3] * 2);

							    strname = Encoding.BigEndianUnicode.GetString(bname);
							    strpwd = Encoding.BigEndianUnicode.GetString(bpwd);
						    }
					    }
				    }
			    }
		    }
	    }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack){
			HttpCookieCollection hcc = Request.Cookies;
			HttpCookie hc = hcc.Get(@"IsSaveUserPWD");
			if (hc != null) {
				string strInfo = hc.Value.ToLower();
				if(strInfo == "true"){
					string strName = string.Empty, strPWD = string.Empty;
					PickupFromCookies(ref strName, ref strPWD);
					if (strName != null && strName.Length > 0)
						txtUserName.Text = strName;
					if (strPWD != null && strPWD.Length > 0)
						txtPassword.Attributes.Add("value", strPWD);
					ckboxRemember.Checked = true;
				}
			}
		}
    }
	protected void btnLogin_Click(object sender, EventArgs e)
	{
		string userName = txtUserName.Text;
		string password = txtPassword.Text;
		string checkStr = txtValidate.Text.ToUpper();
		string sCheckStr = (string)Session["CheckCode"];
		if (checkStr.CompareTo(sCheckStr) == 0)
		{
            MemberAccessor memberSystem = new MemberAccessor();
			string strMD5PWD = OperateControl.GetMD5String(password);
            UInt64 nRet = memberSystem.Validate(userName, strMD5PWD);
            uint uID = (uint)(nRet>>32), uGroupID = (uint)nRet;
            if (uID == 0 || uGroupID == 0)
            {
				Response.Clear();
				this.ClientScript.RegisterStartupScript(GetType(), @"USERPWDERROR", @"<Script Language='JavaScript'>window.alert('用户名密码错误！')</script>"); 
            }
			else if (uID == UInt32.MaxValue || uGroupID == UInt32.MaxValue)
			{
				Response.Clear();
				this.ClientScript.RegisterStartupScript(GetType(), @"USERINVALID", @"<Script Language='JavaScript'>window.alert('用户名已注销！')</script>");
			}
			else
			{
				HttpCookie htc = new HttpCookie(@"IsSaveUserPWD", @"");
				htc.Expires = DateTime.Now.AddDays(1);
				if (ckboxRemember.Checked) htc.Value = @"true";
				Response.Cookies.Set(htc);
				if (ckboxRemember.Checked)
					SaveToCookie(userName, password, 7);
				else {
					htc = new HttpCookie(@"userinfo", @"");
					htc.Expires = DateTime.Now.AddDays(1);
					Response.Cookies.Set(htc);
				}
				Session["CheckCode"] = null;

				Response.Clear();
				switch (uGroupID)
				{
					case 5://MenDian
						Session[@"LogUserId"] = uID.ToString();
						Response.Redirect("Frame2.html");
						break;
					case 3://Customer server
						Session[@"LogUserId"] = uID.ToString();
						Response.Redirect("Frame3.html");
						break;
					case 4://goods send
						Session[@"LogUserId"] = uID.ToString();
						Response.Redirect("Frame4.html");
						break;
					case 1://Administrator
					case 2://Boss
						Session[@"LogUserId"] = uID.ToString();
						Response.Redirect("Frame.html");
						break;
				}
			}
		}
		else
		{
			Response.Clear();
			this.ClientScript.RegisterStartupScript(GetType(), @"INVALIDCODE", @"<Script Language='JavaScript'>window.alert('验证码错误！')</script>");
		}
	}
}
